

import java.applet.Applet;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;

public class TestApplet extends Applet implements ActionListener, ItemListener, TextListener {

	private static final long serialVersionUID = 1L;

	// ActionListener data
	Label buttonEvent = new Label("Button Event", Label.CENTER);
	Button button1 = new Button("Button 1");
	Button button2 = new Button("Button 2");
	Button button3 = new Button("Button 3");

	// ItemListener data
	Label chkboxEvent = new Label("Checkbox Event", Label.CENTER);
	CheckboxGroup group = new CheckboxGroup();
	Checkbox checkbox1 = new Checkbox("Checkbox 1", false, group);
	Checkbox checkbox2 = new Checkbox("Checkbox 2", false, group);
	Checkbox checkbox3 = new Checkbox("Checkbox 3", false, group);

	// TextField
	TextField textField = new TextField("TextField", 20);
	Label textFieldEvent = new Label("TextField Event", Label.CENTER);

	public TestApplet() {

		// ActionListener to handle button events
		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		// Button panel
		Panel buttonPanel = new Panel();
		buttonPanel.add(button1);
		buttonPanel.add(button2);
		buttonPanel.add(button3);
		Panel buttonEventPanel = new Panel();
		buttonEventPanel.add(buttonEvent);

		// ItemListener to handle checkbox events
		checkbox1.addItemListener(this);
		checkbox2.addItemListener(this);
		checkbox3.addItemListener(this);
		// Checkbox panel
		Panel chkboxPanel = new Panel();
		chkboxPanel.add(checkbox1);
		chkboxPanel.add(checkbox2);
		chkboxPanel.add(checkbox3);
		Panel chkboxEventPanel = new Panel();
		chkboxEventPanel.add(chkboxEvent);

		// TextField panel
		textField.addTextListener(this);
		Panel textFieldPanel = new Panel();
		textFieldPanel.add(textField);
		textFieldPanel.add(textFieldEvent);

		// Main layout
		setLayout(new GridLayout(5,1));
		add(buttonPanel);
		add(buttonEventPanel);
		add(chkboxPanel);
		add(chkboxEventPanel);
		add(textFieldPanel);

	}

	// ActionEvent handler (buttons)
	@Override
	public void actionPerformed(ActionEvent event) {
		// Show the button clicked
		Object source = event.getSource();
		if (source == button1) buttonEvent.setText("Button 1");
		else if (source == button2) buttonEvent.setText("Button 2");
		else if (source == button3) buttonEvent.setText("Button 3");
		// Change the button label to "Click"
		((Button) event.getSource()).setLabel("Click");
	}

	// ItemEvent handler (checkboxes)
	@Override
	public void itemStateChanged(ItemEvent event) {
		// Show the checkbox selected
		chkboxEvent.setText((String) event.getItem());
	}

	// TextChanged handler
	public void textValueChanged(TextEvent txt) {
		textFieldEvent.setText(textField.getText());
	}
}
